#include "StdAfx.h"
#include "deadZ80.h"
#include "opcodes.h"

int CDeadZ80::ExecuteOpcodeCB()
{
	unsigned char opcode = ReadMemory8(pc++);
	unsigned short stmp,tmp16;
	unsigned char tmp;
	unsigned long ltmp;

	switch(opcode) {
		case 0x00:	RLC(B);		cycles += 8;	break;
		case 0x01:	RLC(C);		cycles += 8;	break;
		case 0x02:	RLC(D);		cycles += 8;	break;
		case 0x03:	RLC(E);		cycles += 8;	break;
		case 0x04:	RLC(H);		cycles += 8;	break;
		case 0x05:	RLC(L);		cycles += 8;	break;
		case 0x06:	tmp = ReadMemory8(HL);	RLC(tmp);	WriteMemory8(HL,tmp);	cycles += 14;	break;
		case 0x07:	RLC(A);		cycles += 8;	break;
		case 0x08:	RRC(B);		cycles += 8;	break;
		case 0x09:	RRC(C);		cycles += 8;	break;
		case 0x0A:	RRC(D);		cycles += 8;	break;
		case 0x0B:	RRC(E);		cycles += 8;	break;
		case 0x0C:	RRC(H);		cycles += 8;	break;
		case 0x0D:	RRC(L);		cycles += 8;	break;
		case 0x0E:	tmp = ReadMemory8(HL);	RRC(tmp);	WriteMemory8(HL,tmp);	cycles += 14;	break;
		case 0x0F:	RRC(A);		cycles += 8;	break;
		case 0x10:	RL(B);		cycles += 8;	break;
		case 0x11:	RL(C);		cycles += 8;	break;
		case 0x12:	RL(D);		cycles += 8;	break;
		case 0x13:	RL(E);		cycles += 8;	break;
		case 0x14:	RL(H);		cycles += 8;	break;
		case 0x15:	RL(L);		cycles += 8;	break;
		case 0x16:	tmp = ReadMemory8(HL);	RL(tmp);	WriteMemory8(HL,tmp);	cycles += 14;	break;
		case 0x17:	RL(A);		cycles += 8;	break;
		case 0x18:	RR(B);		cycles += 8;	break;
		case 0x19:	RR(C);		cycles += 8;	break;
		case 0x1A:	RR(D);		cycles += 8;	break;
		case 0x1B:	RR(E);		cycles += 8;	break;
		case 0x1C:	RR(H);		cycles += 8;	break;
		case 0x1D:	RR(L);		cycles += 8;	break;
		case 0x1E:	tmp = ReadMemory8(HL);	RR(tmp);	WriteMemory8(HL,tmp);	cycles += 14;	break;
		case 0x1F:	RR(A);		cycles += 8;	break;
		case 0x20:	SLA(B);		cycles += 8;	break;
		case 0x21:	SLA(C);		cycles += 8;	break;
		case 0x22:	SLA(D);		cycles += 8;	break;
		case 0x23:	SLA(E);		cycles += 8;	break;
		case 0x24:	SLA(H);		cycles += 8;	break;
		case 0x25:	SLA(L);		cycles += 8;	break;
		case 0x26:	tmp = ReadMemory8(HL);	SLA(tmp);	WriteMemory8(HL,tmp);	cycles += 14;	break;
		case 0x27:	SLA(A);		cycles += 8;	break;
		case 0x28:	SRA(B);		cycles += 8;	break;
		case 0x29:	SRA(C);		cycles += 8;	break;
		case 0x2A:	SRA(D);		cycles += 8;	break;
		case 0x2B:	SRA(E);		cycles += 8;	break;
		case 0x2C:	SRA(H);		cycles += 8;	break;
		case 0x2D:	SRA(L);		cycles += 8;	break;
		case 0x2E:	tmp = ReadMemory8(HL);	SRA(tmp);	WriteMemory8(HL,tmp);	cycles += 14;	break;
		case 0x2F:	SRA(A);		cycles += 8;	break;
		case 0x30:	SLL(B);		cycles += 8;	break;
		case 0x31:	SLL(C);		cycles += 8;	break;
		case 0x32:	SLL(D);		cycles += 8;	break;
		case 0x33:	SLL(E);		cycles += 8;	break;
		case 0x34:	SLL(H);		cycles += 8;	break;
		case 0x35:	SLL(L);		cycles += 8;	break;
		case 0x36:	tmp = ReadMemory8(HL);	SLL(tmp);	WriteMemory8(HL,tmp);	cycles += 14;	break;
		case 0x37:	SLL(A);		cycles += 8;	break;
		case 0x38:	SRL(B);		cycles += 8;	break;
		case 0x39:	SRL(C);		cycles += 8;	break;
		case 0x3A:	SRL(D);		cycles += 8;	break;
		case 0x3B:	SRL(E);		cycles += 8;	break;
		case 0x3C:	SRL(H);		cycles += 8;	break;
		case 0x3D:	SRL(L);		cycles += 8;	break;
		case 0x3E:	tmp = ReadMemory8(HL);	SRL(tmp);	WriteMemory8(HL,tmp);	cycles += 14;	break;
		case 0x3F:	SRL(A);		cycles += 8;	break;
		case 0x40:	BIT(0,B);	cycles += 4;	break;
		case 0x41:	BIT(0,C);	cycles += 4;	break;
		case 0x42:	BIT(0,D);	cycles += 4;	break;
		case 0x43:	BIT(0,E);	cycles += 4;	break;
		case 0x44:	BIT(0,H);	cycles += 4;	break;
		case 0x45:	BIT(0,L);	cycles += 4;	break;
		case 0x46:	tmp = ReadMemory8(HL);	BIT(0,tmp);	cycles += 4;	break;
		case 0x47:	BIT(0,A);	cycles += 4;	break;
		case 0x48:	BIT(1,B);	cycles += 4;	break;
		case 0x49:	BIT(1,C);	cycles += 4;	break;
		case 0x4A:	BIT(1,D);	cycles += 4;	break;
		case 0x4B:	BIT(1,E);	cycles += 4;	break;
		case 0x4C:	BIT(1,H);	cycles += 4;	break;
		case 0x4D:	BIT(1,L);	cycles += 4;	break;
		case 0x4E:	tmp = ReadMemory8(HL);	BIT(1,tmp);	cycles += 4;	break;
		case 0x4F:	BIT(1,A);	cycles += 4;	break;
		case 0x50:	BIT(2,B);	cycles += 4;	break;
		case 0x51:	BIT(2,C);	cycles += 4;	break;
		case 0x52:	BIT(2,D);	cycles += 4;	break;
		case 0x53:	BIT(2,E);	cycles += 4;	break;
		case 0x54:	BIT(2,H);	cycles += 4;	break;
		case 0x55:	BIT(2,L);	cycles += 4;	break;
		case 0x56:	tmp = ReadMemory8(HL);	BIT(2,tmp);	cycles += 4;	break;
		case 0x57:	BIT(2,A);	cycles += 4;	break;
		case 0x58:	BIT(3,B);	cycles += 4;	break;
		case 0x59:	BIT(3,C);	cycles += 4;	break;
		case 0x5A:	BIT(3,D);	cycles += 4;	break;
		case 0x5B:	BIT(3,E);	cycles += 4;	break;
		case 0x5C:	BIT(3,H);	cycles += 4;	break;
		case 0x5D:	BIT(3,L);	cycles += 4;	break;
		case 0x5E:	tmp = ReadMemory8(HL);	BIT(3,tmp);	cycles += 4;	break;
		case 0x5F:	BIT(3,A);	cycles += 4;	break;
		case 0x60:	BIT(4,B);	cycles += 4;	break;
		case 0x61:	BIT(4,C);	cycles += 4;	break;
		case 0x62:	BIT(4,D);	cycles += 4;	break;
		case 0x63:	BIT(4,E);	cycles += 4;	break;
		case 0x64:	BIT(4,H);	cycles += 4;	break;
		case 0x65:	BIT(4,L);	cycles += 4;	break;
		case 0x66:	tmp = ReadMemory8(HL);	BIT(4,tmp);	cycles += 4;	break;
		case 0x67:	BIT(4,A);	cycles += 4;	break;
		case 0x68:	BIT(5,B);	cycles += 4;	break;
		case 0x69:	BIT(5,C);	cycles += 4;	break;
		case 0x6A:	BIT(5,D);	cycles += 4;	break;
		case 0x6B:	BIT(5,E);	cycles += 4;	break;
		case 0x6C:	BIT(5,H);	cycles += 4;	break;
		case 0x6D:	BIT(5,L);	cycles += 4;	break;
		case 0x6E:	tmp = ReadMemory8(HL);	BIT(5,tmp);	cycles += 4;	break;
		case 0x6F:	BIT(5,A);	cycles += 4;	break;
		case 0x70:	BIT(6,B);	cycles += 4;	break;
		case 0x71:	BIT(6,C);	cycles += 4;	break;
		case 0x72:	BIT(6,D);	cycles += 4;	break;
		case 0x73:	BIT(6,E);	cycles += 4;	break;
		case 0x74:	BIT(6,H);	cycles += 4;	break;
		case 0x75:	BIT(6,L);	cycles += 4;	break;
		case 0x76:	tmp = ReadMemory8(HL);	BIT(6,tmp);	cycles += 4;	break;
		case 0x77:	BIT(6,A);	cycles += 4;	break;
		case 0x78:	BIT(7,B);	cycles += 4;	break;
		case 0x79:	BIT(7,C);	cycles += 4;	break;
		case 0x7A:	BIT(7,D);	cycles += 4;	break;
		case 0x7B:	BIT(7,E);	cycles += 4;	break;
		case 0x7C:	BIT(7,H);	cycles += 4;	break;
		case 0x7D:	BIT(7,L);	cycles += 4;	break;
		case 0x7E:	tmp = ReadMemory8(HL);	BIT(7,tmp);	cycles += 4;	break;
		case 0x7F:	BIT(7,A);	cycles += 4;	break;
		case 0x80:	RES(0,B);	cycles += 4;	break;
		case 0x81:	RES(0,C);	cycles += 4;	break;
		case 0x82:	RES(0,D);	cycles += 4;	break;
		case 0x83:	RES(0,E);	cycles += 4;	break;
		case 0x84:	RES(0,H);	cycles += 4;	break;
		case 0x85:	RES(0,L);	cycles += 4;	break;
		case 0x86:	tmp = ReadMemory8(HL);	RES(0,tmp);	WriteMemory8(HL,tmp);	cycles += 4;	break;
		case 0x87:	RES(0,A);	cycles += 4;	break;
		case 0x88:	RES(1,B);	cycles += 4;	break;
		case 0x89:	RES(1,C);	cycles += 4;	break;
		case 0x8A:	RES(1,D);	cycles += 4;	break;
		case 0x8B:	RES(1,E);	cycles += 4;	break;
		case 0x8C:	RES(1,H);	cycles += 4;	break;
		case 0x8D:	RES(1,L);	cycles += 4;	break;
		case 0x8E:	tmp = ReadMemory8(HL);	RES(1,tmp);	WriteMemory8(HL,tmp);	cycles += 4;	break;
		case 0x8F:	RES(1,A);	cycles += 4;	break;
		case 0x90:	RES(2,B);	cycles += 4;	break;
		case 0x91:	RES(2,C);	cycles += 4;	break;
		case 0x92:	RES(2,D);	cycles += 4;	break;
		case 0x93:	RES(2,E);	cycles += 4;	break;
		case 0x94:	RES(2,H);	cycles += 4;	break;
		case 0x95:	RES(2,L);	cycles += 4;	break;
		case 0x96:	tmp = ReadMemory8(HL);	RES(2,tmp);	WriteMemory8(HL,tmp);	cycles += 4;	break;
		case 0x97:	RES(2,A);	cycles += 4;	break;
		case 0x98:	RES(3,B);	cycles += 4;	break;
		case 0x99:	RES(3,C);	cycles += 4;	break;
		case 0x9A:	RES(3,D);	cycles += 4;	break;
		case 0x9B:	RES(3,E);	cycles += 4;	break;
		case 0x9C:	RES(3,H);	cycles += 4;	break;
		case 0x9D:	RES(3,L);	cycles += 4;	break;
		case 0x9E:	tmp = ReadMemory8(HL);	RES(3,tmp);	WriteMemory8(HL,tmp);	cycles += 4;	break;
		case 0x9F:	RES(3,A);	cycles += 4;	break;
		case 0xA0:	RES(4,B);	cycles += 4;	break;
		case 0xA1:	RES(4,C);	cycles += 4;	break;
		case 0xA2:	RES(4,D);	cycles += 4;	break;
		case 0xA3:	RES(4,E);	cycles += 4;	break;
		case 0xA4:	RES(4,H);	cycles += 4;	break;
		case 0xA5:	RES(4,L);	cycles += 4;	break;
		case 0xA6:	tmp = ReadMemory8(HL);	RES(4,tmp);	WriteMemory8(HL,tmp);	cycles += 4;	break;
		case 0xA7:	RES(4,A);	cycles += 4;	break;
		case 0xA8:	RES(5,B);	cycles += 4;	break;
		case 0xA9:	RES(5,C);	cycles += 4;	break;
		case 0xAA:	RES(5,D);	cycles += 4;	break;
		case 0xAB:	RES(5,E);	cycles += 4;	break;
		case 0xAC:	RES(5,H);	cycles += 4;	break;
		case 0xAD:	RES(5,L);	cycles += 4;	break;
		case 0xAE:	tmp = ReadMemory8(HL);	RES(5,tmp);	WriteMemory8(HL,tmp);	cycles += 4;	break;
		case 0xAF:	RES(5,A);	cycles += 4;	break;
		case 0xB0:	RES(6,B);	cycles += 4;	break;
		case 0xB1:	RES(6,C);	cycles += 4;	break;
		case 0xB2:	RES(6,D);	cycles += 4;	break;
		case 0xB3:	RES(6,E);	cycles += 4;	break;
		case 0xB4:	RES(6,H);	cycles += 4;	break;
		case 0xB5:	RES(6,L);	cycles += 4;	break;
		case 0xB6:	tmp = ReadMemory8(HL);	RES(6,tmp);	WriteMemory8(HL,tmp);	cycles += 4;	break;
		case 0xB7:	RES(6,A);	cycles += 4;	break;
		case 0xB8:	RES(7,B);	cycles += 4;	break;
		case 0xB9:	RES(7,C);	cycles += 4;	break;
		case 0xBA:	RES(7,D);	cycles += 4;	break;
		case 0xBB:	RES(7,E);	cycles += 4;	break;
		case 0xBC:	RES(7,H);	cycles += 4;	break;
		case 0xBD:	RES(7,L);	cycles += 4;	break;
		case 0xBE:	tmp = ReadMemory8(HL);	RES(7,tmp);	WriteMemory8(HL,tmp);	cycles += 4;	break;
		case 0xBF:	RES(7,A);	cycles += 4;	break;
		case 0xC0:	SET(0,B);	cycles += 4;	break;
		case 0xC1:	SET(0,C);	cycles += 4;	break;
		case 0xC2:	SET(0,D);	cycles += 4;	break;
		case 0xC3:	SET(0,E);	cycles += 4;	break;
		case 0xC4:	SET(0,H);	cycles += 4;	break;
		case 0xC5:	SET(0,L);	cycles += 4;	break;
		case 0xC6:	tmp = ReadMemory8(HL);	SET(0,tmp);	WriteMemory8(HL,tmp);	cycles += 4;	break;
		case 0xC7:	SET(0,A);	cycles += 4;	break;
		case 0xC8:	SET(1,B);	cycles += 4;	break;
		case 0xC9:	SET(1,C);	cycles += 4;	break;
		case 0xCA:	SET(1,D);	cycles += 4;	break;
		case 0xCB:	SET(1,E);	cycles += 4;	break;
		case 0xCC:	SET(1,H);	cycles += 4;	break;
		case 0xCD:	SET(1,L);	cycles += 4;	break;
		case 0xCE:	tmp = ReadMemory8(HL);	SET(1,tmp);	WriteMemory8(HL,tmp);	cycles += 4;	break;
		case 0xCF:	SET(1,A);	cycles += 4;	break;
		case 0xD0:	SET(2,B);	cycles += 4;	break;
		case 0xD1:	SET(2,C);	cycles += 4;	break;
		case 0xD2:	SET(2,D);	cycles += 4;	break;
		case 0xD3:	SET(2,E);	cycles += 4;	break;
		case 0xD4:	SET(2,H);	cycles += 4;	break;
		case 0xD5:	SET(2,L);	cycles += 4;	break;
		case 0xD6:	tmp = ReadMemory8(HL);	SET(2,tmp);	WriteMemory8(HL,tmp);	cycles += 4;	break;
		case 0xD7:	SET(2,A);	cycles += 4;	break;
		case 0xD8:	SET(3,B);	cycles += 4;	break;
		case 0xD9:	SET(3,C);	cycles += 4;	break;
		case 0xDA:	SET(3,D);	cycles += 4;	break;
		case 0xDB:	SET(3,E);	cycles += 4;	break;
		case 0xDC:	SET(3,H);	cycles += 4;	break;
		case 0xDD:	SET(3,L);	cycles += 4;	break;
		case 0xDE:	tmp = ReadMemory8(HL);	SET(3,tmp);	WriteMemory8(HL,tmp);	cycles += 4;	break;
		case 0xDF:	SET(3,A);	cycles += 4;	break;
		case 0xE0:	SET(4,B);	cycles += 4;	break;
		case 0xE1:	SET(4,C);	cycles += 4;	break;
		case 0xE2:	SET(4,D);	cycles += 4;	break;
		case 0xE3:	SET(4,E);	cycles += 4;	break;
		case 0xE4:	SET(4,H);	cycles += 4;	break;
		case 0xE5:	SET(4,L);	cycles += 4;	break;
		case 0xE6:	tmp = ReadMemory8(HL);	SET(4,tmp);	WriteMemory8(HL,tmp);	cycles += 4;	break;
		case 0xE7:	SET(4,A);	cycles += 4;	break;
		case 0xE8:	SET(5,B);	cycles += 4;	break;
		case 0xE9:	SET(5,C);	cycles += 4;	break;
		case 0xEA:	SET(5,D);	cycles += 4;	break;
		case 0xEB:	SET(5,E);	cycles += 4;	break;
		case 0xEC:	SET(5,H);	cycles += 4;	break;
		case 0xED:	SET(5,L);	cycles += 4;	break;
		case 0xEE:	tmp = ReadMemory8(HL);	SET(5,tmp);	WriteMemory8(HL,tmp);	cycles += 4;	break;
		case 0xEF:	SET(5,A);	cycles += 4;	break;
		case 0xF0:	SET(6,B);	cycles += 4;	break;
		case 0xF1:	SET(6,C);	cycles += 4;	break;
		case 0xF2:	SET(6,D);	cycles += 4;	break;
		case 0xF3:	SET(6,E);	cycles += 4;	break;
		case 0xF4:	SET(6,H);	cycles += 4;	break;
		case 0xF5:	SET(6,L);	cycles += 4;	break;
		case 0xF6:	tmp = ReadMemory8(HL);	SET(6,tmp);	WriteMemory8(HL,tmp);	cycles += 4;	break;
		case 0xF7:	SET(6,A);	cycles += 4;	break;
		case 0xF8:	SET(7,B);	cycles += 4;	break;
		case 0xF9:	SET(7,C);	cycles += 4;	break;
		case 0xFA:	SET(7,D);	cycles += 4;	break;
		case 0xFB:	SET(7,E);	cycles += 4;	break;
		case 0xFC:	SET(7,H);	cycles += 4;	break;
		case 0xFD:	SET(7,L);	cycles += 4;	break;
		case 0xFE:	tmp = ReadMemory8(HL);	SET(7,tmp);	WriteMemory8(HL,tmp);	cycles += 4;	break;
		case 0xFF:	SET(7,A);	cycles += 4;	break;
		default:
			message("bad CB opcode $%02X\n",opcode);
			return(1);
	}
	return(0);
}
